package edu.gatech.gro.model.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import edu.gatech.gro.model.GroceryStore;

public class GroceryStoreJsonHandler extends JsonHandler {

	private static final String TAG = "GROCERY_STORE_JSON_HANDLER";

	@Override
	protected void parseDataArray() {
		List<GroceryStore> dataParsed = new ArrayList<GroceryStore>();
		try {
			JSONArray array = json.getJSONArray("data");
			for (int i = 0; i < array.length(); i++) {
				dataParsed.add(getGroceryStore(array.getJSONObject(i)));
			}
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage());
		}
		getResponse().setData(dataParsed);
	}

	@Override
	protected void parseData() {
		GroceryStore dataParsed = null;
		try {
			dataParsed = getGroceryStore(json.getJSONObject("data"));
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage());
		}
		getResponse().setData(dataParsed);
	}

	public GroceryStore getGroceryStore(JSONObject obj) {
		GroceryStore gs = new GroceryStore();
		try {
			parseAbstractObject(gs, obj);
			gs.setName(obj.getString("name"));
			gs.setNameClean(obj.getString("nameClean"));
		} catch (JSONException e) {
			Log.e(TAG, e.getMessage());
		}
		return gs;
	}
}
